

#include "swordsman.h"
#include "gamemanager.h"
#include "CCSkeletalSprite.h"
#include "bullet.h"
#include "army.h"
#include "gamescene.h"

#define __super         Unit

Swordsman::Swordsman() {

}

Swordsman::~Swordsman() {

}

void Swordsman::init(UnitInfo const &info, float unitScale) {
	__super::init(info, unitScale);
}

void Swordsman::update(float ticks) {
	__super::update(ticks);
}

void Swordsman::handleStateWalkAttack(float ticks) {
	move(ticks);

	if (sprite->animationIsDone()) {
		if (!target) {
			if (arrivedTargetPosition) {
				transformState(UnitState::idle);
			} else {
				transformState(UnitState::walk);
			}
		} else {
			if(arrivedTargetPosition) {
				transformState(UnitState::idle);
			} else {
				transformState(UnitState::walk);
			}
		}
	}
}

void Swordsman::handleStateWalkAim(float ticks) {

}

void Swordsman::handleStateWalk(float ticks) {
	if(gameScene->battleState == BattleState::standby) {
		transformState(UnitState::idle);
	} else {
		move(ticks);

		if(arrivedTargetPosition) {
			transformState(UnitState::idle);
		}
	}
}

void Swordsman::handleStateIdle(float ticks) {
	if(gameScene->battleState == BattleState::standby) {
		return;
	}

	if (_timeElapsed >= attribute.reloadTime) {
		if(gridInfo.globalLineIndex == ourArmy->frontLine && target) {
			transformState(UnitState::attack);
		}
	}
}

void Swordsman::handleStateDying(float ticks) {
	if (sprite->animationIsDone()) {
		transformState(UnitState::died);
	}
}

void Swordsman::handleStateAim(float ticks) {

}

void Swordsman::handleStateAttack(float ticks) {
	if (sprite->animationIsDone()) {
		transformState(UnitState::idle);
	}
}

void Swordsman::handleStateBirth(float ticks) {
	transformState(UnitState::idle);
}

void Swordsman::handleStateAssign(float ticks) {
	move(ticks);

	if(arrivedTargetPosition) {
		transformState(UnitState::idle);
	}
}

void Swordsman::onEnterState(UnitState theState) {
	__super::onEnterState(theState);

	if(theState == UnitState::walk_reload) {
		sprite->playAnimation("walk");
		sprite->setLoopCount(-1);
	} else if(theState == UnitState::reload) {
		sprite->playAnimation("idle");
		sprite->setLoopCount(-1);
	}
}
